//
//  ChannelHeaderView.m
//  NewsChannal
//
//  Created by chinabyte on 2017/7/18.
//  Copyright © 2017年 chinabyte. All rights reserved.
//

#import "ChannelHeaderView.h"
#import "NewsChannelEdit.h"
#import "UIView+Extension.h"

//设置字体
#define TTFont(f) [UIFont systemFontOfSize:(f)]
//默认间距为10
NSInteger const kDefaultSpace = 10;
//编辑按钮的宽度
const CGFloat editBtnW = 50;
//编辑按钮的宽度
const CGFloat editBtnH = 30;

@interface ChannelHeaderView ()
@property (nonatomic, weak) UILabel *channelView;
@property (nonatomic, weak) UILabel *promptView; // 提示view
@property (nonatomic, weak) UIButton *editBtn;
@end

@implementation ChannelHeaderView
- (UIButton *)editBtn{
    if (!_editBtn) {
        UIButton *editBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [editBtn setTitle:@"编辑" forState:UIControlStateNormal];
        [editBtn setTitle:@"完成" forState:UIControlStateSelected];
        [editBtn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        [editBtn setTitleColor:[UIColor redColor] forState:UIControlStateSelected];
        editBtn.titleLabel.font = TTFont(15);
        editBtn.layer.borderColor = [UIColor redColor].CGColor;
        editBtn.layer.cornerRadius = 15;
        editBtn.layer.borderWidth = 0.5;
        [editBtn addTarget:self action:@selector(editBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:editBtn];
        _editBtn = editBtn;
    }
    return _editBtn;
}
- (UILabel *)channelView{
    if (!_channelView) {
        UILabel *channelView = [UILabel new];
        channelView.textColor = [UIColor blackColor];
        channelView.font = TTFont(17);
        [channelView sizeToFit];
        [self addSubview:channelView];
        _channelView = channelView;
    }
    return _channelView;
}

- (UILabel *)promptView{
    if (!_promptView) {
        UILabel *promptView = [UILabel new];
        promptView.textColor = [UIColor grayColor];
        promptView.font = TTFont(12);
        [promptView sizeToFit];
        [self addSubview:promptView];
        _promptView = promptView;
    }
    return _promptView;
}

- (void)layoutSubviews{
    [super layoutSubviews];
    self.backgroundColor = [UIColor whiteColor];
    
    CGFloat height = self.frame.size.height;
    CGFloat width = self.frame.size.width;
    
    self.channelView.tt_x = kDefaultSpace;
    self.channelView.tt_y = 0;
    self.channelView.tt_height = height;
    
    self.promptView.tt_x = CGRectGetMaxX(self.channelView.frame) + kDefaultSpace;
    self.promptView.tt_y = 0;
    self.promptView.tt_height = self.frame.size.height;
    
    self.editBtn.tt_x = width - kDefaultSpace - editBtnW;
    self.editBtn.tt_height = editBtnH;
    self.editBtn.tt_width = editBtnW;
    self.editBtn.tt_centerY = height/2.0;
    
}
- (void)editBtnClick:(UIButton *)sender{
    sender.selected = !sender.selected;
    self.channelEdit.isEditing = sender.selected; // 更新编辑状态
    self.promptView.text = self.channelEdit.isEditing?self.channelEdit.promptEditTitle:self.channelEdit.promptTitle; // 更新提示标题
    if ([self.ttDelegate respondsToSelector:@selector(ChannelHeaderView:didClickEditButton:)]) {
        [self.ttDelegate ChannelHeaderView:self didClickEditButton:self.channelEdit.isEditing];
    }
}

#pragma mark 重写属性赋值
- (void)setChannelEdit:(NewsChannelEdit *)channelEdit{
    _channelEdit = channelEdit;
    self.channelView.text = channelEdit.channelTitle;
    [self.channelView sizeToFit];
    self.editBtn.hidden = channelEdit.hideEditView;
    self.promptView.text = channelEdit.isEditing?channelEdit.promptEditTitle:channelEdit.promptTitle;
    [self.promptView sizeToFit];
    self.editBtn.selected = channelEdit.isEditing;
}
@end
